package servlet;

import dao.UserWordDao;
import dao.impl.UserWordDaoImpl;
import entity.UserWord;
import utils.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "GetMessageServlet", urlPatterns = "/GetMessageServlet")

public class GetMessageServlet extends HttpServlet {

    //修改密码
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserWordDao usersDao = new UserWordDaoImpl();
        // 设置响应的内容类型为 JSON
        resp.setContentType("application/json");
        //设置ContentType字段值
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer userId = Integer.valueOf(req.getParameter("userId"));
        List<UserWord> allRecordByUserId = usersDao.getMonthRecordByUserId(userId);
        Integer count = allRecordByUserId.size();
        //本月总背单词的时长
        Long allTime = 0L;
        for (UserWord userWord : allRecordByUserId) {
            allTime += userWord.getTime();
        }
        Map<String, Object> result = new HashMap<>();
        result.put("count", count);
        result.put("allTime", allTime);

        //返回处理过的数据
        resp.getWriter().write(ResponseResult.success(result).toString());
    }
}
